package com.wuchengdong.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wuchengdong.web.extend.utils.R;
import com.wuchengdong.web.pojo.Notification;
import com.wuchengdong.web.service.NotificationService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/notification")
public class NotificationController {
    @Autowired
    NotificationService notificationService;

    @GetMapping("/server")
    @ApiOperation(value = "由服务端发起，获取用户发来的消息，'0'代表服务端id")
    public R<List<Notification>> getForServer() {
        List<Notification> list = notificationService.list(
                new LambdaQueryWrapper<Notification>()
                        .eq(Notification::getToId, 0)
                        .eq(Notification::getIsRead,false)
                        .orderByAsc(Notification::getCreatedAt)
        );
        for (Notification notification : list) {
            notificationService.update(
                    new LambdaUpdateWrapper<Notification>().eq(Notification::getId,notification.getId()).set(Notification::getIsRead,true)
            );
        }
        return R.success(list);
    }

    @GetMapping("/user")
    @ApiOperation(value = "由用户端发起，获取服务端消息(未读消息)，'0'代表服务端id")
    public R<List<Notification>> getForUser(Integer userId) {
        List<Notification> list = notificationService.list(
                new LambdaQueryWrapper<Notification>()
                        .eq(Notification::getFromId,0)
                        .eq(Notification::getToId, userId)
                        .eq(Notification::getIsRead,false)
                        .orderByAsc(Notification::getCreatedAt)
        );
        for (Notification notification : list) {
            notificationService.update(
                    new LambdaUpdateWrapper<Notification>().eq(Notification::getId,notification.getId()).set(Notification::getIsRead,true)
            );
        }
        return R.success(list);
    }

    @PostMapping("/send")
    @ApiOperation(value = "发送消息，'0'代表服务端id")
    public R<String> send(@RequestBody Notification notification) {
        notificationService.save(notification);
        return R.success("发送成功");
    }

    @PutMapping("/read")
    @ApiOperation(value = "标记消息已读")
    public R<String> read(Notification notification) {
        if (notification.getId() == null) {
            return R.error("消息ID为空");
        }
        notificationService.update(
                new LambdaUpdateWrapper<Notification>().eq(Notification::getId,notification.getId()).set(Notification::getIsRead,true)
        );
        return R.success("已读");
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "不建议使用，请建立适合的消息过期机制，而不是使用本接口单个删除")
    public R<String> delete(Notification notification) {
        if (notification.getId() == null) {
            return R.error("消息ID为空");
        }
        notificationService.removeById(notification.getId());
        return R.success("删除成功");
    }
}
